home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / DRAGLIST.PAK / DRAGLISX.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  275 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/static.h>
  9. #include <owl/draglist.h>
  10. #include <owl/radiobut.h>
  11. #include <stdio.h>
  12.  
  13. const int DragListId1 = 100;
  14. const int DragListId2 = 101;
  15. const int CopyButtonId= 102;
  16. const int MoveButtonId= 103;
  17. TStatic* Status;
  18. char Buffer[512];
  19.  
  20. //
  21. // class TExampleDragList
  22. // ~~~~~ ~~~~~~~~~~~~~~~~
  23. class _USERCLASS TExampleDragList : public TDragList {
  24.   public:
  25.     // Constructors
  26.     //
  27.     TExampleDragList(TWindow* parent,int id,int x, int y, int w, int h,
  28.                      TModule* module = 0)
  29.     :
  30.       TDragList(parent, id, x, y, w, h, module)
  31.     {
  32.       Attr.Style &= ~LBS_SORT;
  33.       Copy = true;
  34.     }
  35.  
  36.     TExampleDragList(TWindow* parent, int resourceId, TModule* module = 0)
  37.     :
  38.       TDragList(parent, resourceId, module)
  39.     {
  40.       Copy = true;
  41.     }
  42.  
  43.     bool BeginDrag(int item, const TPoint&);
  44.     TCursorType Dragging(int item, const TPoint&);
  45.     void Dropped(int item, const TPoint& point);
  46.     void CancelDrag(int item, const TPoint&);
  47.     void SetCopy(bool newValue)
  48.     {
  49.       Copy= newValue;
  50.     }
  51.  
  52.   private:
  53.     int StartItem;
  54.     bool Copy;
  55. };
  56.  
  57. bool
  58. TExampleDragList::BeginDrag(int item, const TPoint&)
  59. {
  60.    StartItem = item;
  61.  
  62.    if (StartItem == 0)
  63.       return false;
  64.    sprintf(::Buffer, "Start item %d", StartItem);
  65.  
  66.    if (::Status)
  67.       ::Status->SetText(::Buffer);
  68.    return true;
  69. }
  70.  
  71. TDragList::TCursorType
  72. TExampleDragList::Dragging(int item, const TPoint& point)
  73. {
  74.    HWND hwnd=WindowFromPoint(point);
  75.    if (hwnd == *(Parent->ChildWithId(DragListId1)) || 
  76.             hwnd == *(Parent->ChildWithId(DragListId2)) ) {
  77.       if (hwnd != *this) {
  78.          if (Copy)
  79.             sprintf(::Buffer, "Copying to top of other ListBox");
  80.          else
  81.             sprintf(::Buffer, "Move to top of other ListBox");
  82.       }
  83.       else if (item == 0) {
  84.          sprintf(::Buffer, "Cannot insert new item here");  
  85.       }
  86.       else {
  87.          if (item == -1)
  88.             sprintf(::Buffer, "Move to end of list");
  89.          else
  90.             sprintf(::Buffer, "Moving to item %d", item);
  91.       }
  92.  
  93.       if (::Status)
  94.          ::Status->SetText(::Buffer);
  95.  
  96.       if (Copy && (hwnd != *this) )
  97.          return TDragList::dlCopy;
  98.       else if (item==0)
  99.          return TDragList::dlStop;
  100.       else
  101.          return TDragList::dlMove;
  102.    }
  103.    sprintf(::Buffer, "Cancel");
  104.    if (::Status)
  105.       ::Status->SetText(::Buffer);
  106.    return TDragList::dlMove;
  107. }
  108.  
  109. void
  110. TExampleDragList::Dropped(int item, const TPoint& point)
  111. {
  112.    if ( (StartItem != 0) && (StartItem != item) && (item != 0) ) {
  113.       int len= GetStringLen(StartItem);
  114.       char *moveItem= new char[len+1];
  115.       if (moveItem) {
  116.          GetString(moveItem, StartItem);
  117.          HWND hwnd = WindowFromPoint(point);
  118.          if (hwnd == *(Parent->ChildWithId(DragListId1)) || 
  119.             hwnd == *(Parent->ChildWithId(DragListId2)) ) {
  120.          if (!Copy || (hwnd == *this) )
  121.             DeleteString(StartItem);
  122.          if (hwnd == *this) {
  123.             if (item != -1)
  124.                sprintf(::Buffer, "Dragged from %d to %d", StartItem, item);
  125.             else
  126.                sprintf(::Buffer, "Dragged to end of list");
  127.             InsertString(moveItem, item);
  128.          }
  129.          else {
  130.             sprintf(::Buffer, "Dragged from %d to the top of the other ListBox", StartItem);
  131.             ::SendMessage(hwnd, LB_INSERTSTRING, 1, (LPARAM)((LPSTR)moveItem));
  132.          }
  133.          }
  134.       }
  135.       if (::Status)
  136.          ::Status->SetText(::Buffer);
  137.  
  138.       delete[] moveItem;
  139.    }
  140. }
  141.  
  142. void
  143. TExampleDragList::CancelDrag(int, const TPoint&)
  144. {
  145.    sprintf(::Buffer, "Cancelled drag");
  146.  
  147.    if (::Status)
  148.       ::Status->SetText(::Buffer);
  149. }
  150.  
  151.  
  152.  
  153. //
  154. // class TDragListWindow
  155. // ~~~~~ ~~~~~~~~~~~~~~~
  156. class _USERCLASS TDragListWindow : public TWindow, public TDragListEventHandler {
  157.   public:
  158.     TDragListWindow();
  159.     void SetupWindow();
  160.     // EV_COMMAND_AND_ID function- void ButtonClicked(WPARAM id);
  161.     void CopyClicked();
  162.     void MoveClicked();
  163.  
  164.   private:
  165.     TExampleDragList* DragList1;
  166.     TExampleDragList* DragList2;
  167.     TRadioButton* CopyButton;
  168.     TRadioButton* MoveButton;
  169.  
  170.   DECLARE_RESPONSE_TABLE(TDragListWindow);
  171. };
  172.  
  173. DEFINE_RESPONSE_TABLE2(TDragListWindow, TWindow, TDragListEventHandler)
  174.    // EV_COMMAND_AND_ID(CopyButtonId,ButtonClicked),
  175.    // EV_COMMAND_AND_ID(MoveButtonId,ButtonClicked),
  176.    EV_BN_CLICKED(CopyButtonId, CopyClicked),
  177.    EV_BN_CLICKED(MoveButtonId, MoveClicked),
  178. END_RESPONSE_TABLE;
  179.  
  180. TDragListWindow::TDragListWindow() 
  181.    TWindow(0, 0, 0) 
  182. {
  183.    ::Status = new TStatic(this, -1, "", 10, 0, 410, 35);
  184.    DragList1 = new TExampleDragList(this, DragListId1, 10, 60, 210, 160);
  185.    DragList2 = new TExampleDragList(this, DragListId2, 230, 60, 210, 160);
  186.    CopyButton= new TRadioButton(this, CopyButtonId, "Copy Entry", 115, 235, 100, 20);
  187.    MoveButton= new TRadioButton(this, MoveButtonId, "Move Entry", 255, 235, 100, 20);
  188. }
  189.  
  190. void 
  191. TDragListWindow::SetupWindow() 
  192. {
  193.    TWindow::SetupWindow();
  194.    CopyButton->SetCheck(BF_CHECKED);
  195.    DragList1->SetCopy(true);
  196.    DragList2->SetCopy(true);
  197.  
  198.    // Add items to listbox
  199.    //
  200.    DragList1->AddString("Cannot be dragged");
  201.    DragList1->AddString("Amber");
  202.    DragList1->AddString("Annie");
  203.    DragList1->AddString("Anh");
  204.    DragList1->AddString("Della");
  205.    DragList1->AddString("Doreen");
  206.    DragList1->AddString("Elizabeth");
  207.    DragList1->AddString("Eva");
  208.    DragList1->AddString("Della");
  209.    DragList1->AddString("Jean");
  210.    DragList1->AddString("Josephine");
  211.    DragList1->AddString("Maya");
  212.    DragList1->AddString("Soyeun");
  213.    DragList1->AddString("Sally");
  214.    DragList1->AddString("Valerie");
  215.  
  216.    DragList2->AddString("Cannot be dragged");
  217.    DragList2->AddString("Chris");
  218.    DragList2->AddString("Tommy");
  219.    DragList2->AddString("Bruneau");
  220.    DragList2->AddString("Jason");
  221.    DragList2->AddString("Dave");
  222.    DragList2->AddString("Carl");
  223. }
  224.  
  225. /* EV_COMMAND_AND_ID function- 
  226. void 
  227. TDragListWindow::ButtonClicked(WPARAM id)
  228. {
  229.    if(id== CopyButtonId) {
  230.       DragList1->SetCopy(true);
  231.       DragList2->SetCopy(true);
  232.    }
  233.    else {
  234.       DragList1->SetCopy(false);
  235.       DragList2->SetCopy(false);
  236.    }
  237. }  */
  238.  
  239. void 
  240. TDragListWindow::CopyClicked()
  241. {
  242.    DragList1->SetCopy(true);
  243.    DragList2->SetCopy(true);
  244. }
  245.  
  246. void 
  247. TDragListWindow::MoveClicked()
  248. {
  249.    DragList1->SetCopy(false);
  250.    DragList2->SetCopy(false);
  251. }
  252.  
  253. //
  254. // class TDragListApp
  255. // ~~~~~ ~~~~~~~~~~~~
  256. class _USERCLASS TDragListApp : public TApplication {
  257.   public:
  258.     void InitMainWindow();
  259. };
  260.  
  261. void
  262. TDragListApp::InitMainWindow()
  263. {
  264.   TFrameWindow* frame = new TFrameWindow(0, "DragList Example",
  265.     new TDragListWindow);
  266.   SetMainWindow(frame);
  267. }
  268.  
  269. int
  270. OwlMain(int /*argc*/, char* /*argv*/ [])
  271. {
  272.   return TDragListApp().Run();
  273. }
  274.